package cn.pzhu.services.admin;

import cn.pzhu.model.College;
import cn.pzhu.model.Role;
import cn.pzhu.services.BaseServices;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dongp on 2016/4/25.
 */
public class CollegeServices extends BaseServices {

    public List<College> ajaxQuery() {
        List<College> collegeList = College.dao.find("select * from college");
        return collegeList;
    }

    public List<College> ajaxQueryPage(String pageNumber, String pageSize, String search) {
        ArrayList arrts = new ArrayList();
        Map<String,Object> map = new HashMap<>();

        List<College> colleges = College.dao.find("select c.* from colleges c where c.name like '%" + search + "%' limit "
                + pageNumber + "," + pageSize);
        arrts.add(colleges);

        College college = College.dao.findFirst("select count(*) total from colleges c where c.name like '%"+search+"%'");
        Long total = college.getLong("total").longValue();
        map.put("total", total);
        arrts.add(map);
        return arrts;
    }

    public boolean ajaxUpdate(int id, String name, int gain, int experts, String orgAttr, String researchArea) {

        boolean result = College.dao.findById(id).set("name",name).set("gain",gain).set("experts", experts)
                .set("orgAttr", orgAttr).set("researchArea",researchArea).update();

        return result;
    }

    public boolean ajaxAdd(String name,int gain, int experts, String orgAttr, String researchArea) {

        // 检查是否重复
        College college =  College.dao.findFirst("select count(*) count from colleges where name = ?",name);
        Long count = college.getLong("count").longValue();
        if(count > 0) {
            return false;
        }

        // 保存
        boolean result = new College().set("name", name).set("gain",gain).set("experts",experts).set("orgAttr",orgAttr)
                .set("researchArea",researchArea).save();
        return result;
    }

    public boolean ajaxDel(String id) {
        return College.dao.deleteById(id);
    }

}
